#!/usr/bin/env python
import os, commands, sys

check_services = { 
                  # 'Appication name', 'Monit name', 'pid file', 'process name', 'check'
                    "itop" : ['itop', 'itop', '/var/run/itop.pid', 'itop-app', '/etc/monit/conf.d/itop.monitrc'],
                    "ncare" : ['ncare', 'ncare','/var/run/ncare.pid', 'ncare-app', '/etc/monit/conf.d/ncare.monitrc'],
                    "searchengine" : ['searchengine', 'searchengine','/var/run/searchengine.pid', 'searchengine', '/etc/monit/conf.d/searchengine.monitrc'],
                    "rdp2swf" : ['rdp2swf', 'rdp2swf','/var/run/rdp2swf.pid', 'rdp2swf', '/etc/monit/conf.d/rdp2swf.monitrc'],
                    "tunnelsshd" :['tunnelsshd', 'tunnelsshd','/var/run/tunnelsshd.pid', 'tunnelsshd', '/etc/monit/conf.d/tunnelsshd.monitrc'],
                }


def process_status (pid_file, process_string):
    try:
        pf = open(pid_file,'r')
        pid = int(pf.readline().strip())
        pf.close()
        #print "pid - ", pid
        if pid > 0:
            try:
                proc_file = "/proc/%s/cmdline" % pid
                if not os.path.isfile(proc_file):
                    return False
                fp = open(proc_file, "r")
                line = fp.readline()
                fp.close()
                #print "line - ", line
                if line.find(process_string) != -1:
                    return True
            except:
                return False
    except Exception:
        return False
    return False
    
argvLen = len(sys.argv)
if argvLen != 2:
    print "Usage: %s <service-name>"% sys.argv[0]
    sys.exit(4)

key = sys.argv[1].strip()


if key == "":
    print "Service should not be empty string"
    sys.exit(4)

try:
    if check_services.has_key(key):
        service = check_services[key]
        service_name = service[0]
        monit_service = service[1]
        pid_file = service[2]
        process_string = service[3]
        check_file = service[4]
        if check_file == '' or os.path.isfile(check_file):
            is_running = process_status(pid_file, process_string)
            if not is_running:
                print "Service %s is not running" % service[0]
                sys.exit(2)
            else:
                print "Service %s is running" % service[0]
                sys.exit(1)
        else:
            print "Service %s is not configured in this node" % service[0]
            sys.exit(3)
    elif key == "db":
        if os.path.isdir("/opt/sql"):
            if commands.getstatusoutput("/usr/bin/pgrep -x mysqld")[0] == 0:
                print "Service %s is running" % key
                sys.exit(1)
            else:
                print "Service %s is not running" % key
                sys.exit(2)
        else:
            print "Service %s is not configured in this node" % key
            sys.exit(3)
            
    elif key == "activemq":
        if os.path.isdir("/opt/activemq"):
            if commands.getstatusoutput("/bin/ps axo command | /bin/grep activemq |  /bin/grep -v grep | /bin/grep -v %s" % sys.argv[0])[0] == 0:
                print "Service %s is running" % key
                sys.exit(1)
            else:
                print "Service %s is not running" % key
                sys.exit(2)
        else:
            print "Service %s is not configured in this node" % key
            sys.exit(3)

    elif key == "kafka":
        if os.path.isdir("/opt/kafka"):
            if commands.getstatusoutput("/usr/bin/service kafka status")[0] == 0:
                print "Service %s is running" % key
                sys.exit(1)
            else:
                print "Service %s is not running" % key
                sys.exit(2)
        else:
            print "Service %s is not configured in this node" % key
            sys.exit(3)

    elif key == "zk":
        if os.path.isdir("/opt/kafka"):
            if commands.getstatusoutput("/usr/bin/service kafka status")[0] == 0:
                print "Service %s is running" % key
                sys.exit(1)
            else:
                print "Service %s is not running" % key
                sys.exit(2)
        else:
            print "Service %s is not configured in this node" % key
            sys.exit(3)
    
    elif key == "cassandra":
        if os.path.isdir("/opt/sql"):
            if commands.getstatusoutput("/bin/ps axo command | /bin/grep cassandra.pid |  /bin/grep -v grep | /bin/grep -v %s" % sys.argv[0])[0] == 0:
                print "Service %s is running" % key
                sys.exit(1)
            else:
                print "Service %s is not running" % key
                sys.exit(2)
        else:
            print "Service %s is not configured in this node" % key
            sys.exit(3)
    
    elif key == "mongodb":
        if os.path.isdir("/opt/sql"):
            if commands.getstatusoutput("/usr/bin/pgrep -x mongod")[0] == 0:
                print "Service %s is running" % key
                sys.exit(1)
            else:
                print "Service %s is not running" % key
                sys.exit(2)
        else:
            print "Service %s is not configured in this node" % key
            sys.exit(3)
    else:
        print "Unknown service"
        sys.exit(4)

            
except Exception, msg:
    print "Service %s: Exception - %s " % (service[0],msg)
    sys.exit(4)


